﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kovan.BLL.UserCenter;
using Kovan.Model.Entity.UserCenter;
using Kovan.Model.Common;
using Kovan.Model.Parameter.UserCenter;
using Kovan.Model.ViewModel;
using Kovan.Library.Json;
using Kovan.BLL.Common;

namespace Kovan.UI.Areas.Manage.Controllers
{
    public class ModuleController : ManageControllerBase
    {
        //
        // GET: /Manage/Module/

        public ActionResult Index()
        {
            var list = new List<EMenu>();
            BActModule.GetInstances().LoadMenu(new PModule { application_id = WebConfig.CurrentUser.ApplicationID, parent_id = 0 }, list);

            ViewBag.AuthorityCodes = BActAuthority.GetInstances().GetAuthorityCode();
            //if (list != null)
            //{
            //list = list.OrderByDescending(c => c.module.sort_order).ToList();
            //}
            return PartialView("index", list);
        }

        /// <summary>
        /// 快速保存导航
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public ActionResult SaveNav(long? module_id, string name, int? sort_order)
        {
            ResultCode<string> code = new ResultCode<string>();
            int result = 0;

            string statuscode = string.Empty;

            if (module_id != null && module_id.HasValue)
            {
                var model = BActModule.GetInstances().GetModel(module_id.Value);
                model.name = name;
                model.sort_order = sort_order;
                result = BActModule.GetInstances().Update(model);
            }

            if (result > 0)
            {
                //成功
                statuscode = GetSucceedCode("保存成功", "module_manage", Url.Content("~/Manage/Module"), "菜单管理");
            }
            else
            {
                //失败
                statuscode = GetFailCode("保存失败", "", "", "");
            }
            return Content(statuscode);
        }

        /// <summary>
        /// 保存菜单
        /// </summary>
        /// <param name="model"></param>
        /// <param name="authority"></param>
        /// <returns></returns>
        public ActionResult SaveMenu(act_module model, string authoritys)
        {
            int result = 0;

            string statuscode = string.Empty;
            if (model.module_id.HasValue && model.module_id.Value > 0)
            {
                var model1 = BActModule.GetInstances().GetModel(model.module_id.Value);
                model1.enabled = model.enabled ?? 0;
                model1.front_url = model.front_url;
                model1.icon_url = model.icon_url;
                model1.is_menu = model.is_menu ?? 0;
                model1.is_view = model.is_view ?? 0;
                model1.menu_params = model.menu_params;
                model1.name = model.name;
                model1.sort_order = model.sort_order;
                model1.parent_id = model.parent_id;
                result = BActModule.GetInstances().Update(model1);

                //删除菜单权限
                BActAuthority.GetInstances().DeleteByModuleId(model.module_id.Value);
            }
            else
            {
                model.application_id = 1;
                result = BActModule.GetInstances().Add(model);
            }

            //添加菜单权限
            if (!string.IsNullOrEmpty(authoritys))
            {
                List<string> codes = authoritys.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                BActAuthority.GetInstances().BatchAdd(model.module_id.Value, codes);
            }

            if (result > 0)
            {
                //成功
                statuscode = GetSucceedCode("保存成功", "module_manage", Url.Content("~/Manage/Module"), "菜单管理");
            }
            else
            {
                //失败
                statuscode = GetFailCode("保存失败", "", "", "");
            }
            return Content(statuscode);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DeleteMenu(string ids, bool? isNav)
        {
            if (isNav != null && isNav.Value)
            {
                var elist = BActModule.GetInstances().GetList(1);
                List<string> idss = new List<string>();
                idss.Add(ids);
                idss.AddRange(BActModule.GetInstances().GetSonModuleIds(Convert.ToInt64(ids), elist));
                ids = string.Join(",", idss);
            }

            ResultCode<string> code = new ResultCode<string>();
            int result = 0;
            string statuscode = string.Empty;
            if (!string.IsNullOrEmpty(ids))
            {
                var attr = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                result = BActModule.GetInstances().BatchDelete(attr);
                if (result > 0)
                {
                    statuscode = GetSucceedCode("删除成功", "module_manage", Url.Content("~/Manage/Module"), "菜单管理");
                }
                else
                {
                    statuscode = GetFailCode("删除失败", "", "", "");
                }
            }
            return Content(statuscode);
        }

        /// <summary>
        /// 获取菜单实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ContentResult GetMenu(long id)
        {
            ResultCode<act_module> rc = new ResultCode<act_module>();
            var model = BActModule.GetInstances().GetModel(id);
            if (model != null)
            {
                var authoritys = BActAuthority.GetInstances().GetListByModuleId(model.module_id.Value);
                rc.Description = authoritys.Count() > 0 ? string.Join(",", authoritys.Select(c => c.code)) : "";
                //rc.Description.Split
                rc.Status = true;
                rc.statusCode = "200";
                rc.Data = model;


            }
            else
            {
                rc.Status = false;
            }

            return Content(rc.ToJson());
        }

        public ActionResult MenuAdd(long? id, long? parentid)
        {
            act_module model = new act_module();
            if (id != null && id.HasValue && id.Value > 0)
            {
                model = BActModule.GetInstances().GetModel(id.Value);
                ViewBag.Authoritys = BActAuthority.GetInstances().GetListByModuleId(id.Value);

                ViewBag.IsUpdate = true;
            }
            if (parentid != null && parentid.HasValue)
            {
                ViewBag.ParentId = parentid;

            }

            ViewBag.AuthorityCodes = BActAuthority.GetInstances().GetAuthorityCode();

            var list = new List<EMenu>();
            BActModule.GetInstances().LoadMenu(new PModule { application_id = 1, parent_id = 0 }, list);
            ViewBag.EMenu = list;

            return PartialView("MenuAddDialog", model);
        }
    }
}
